#################################################
#
# Makefile progetto lso 2010
# (fram 1)(fram 2)
#################################################

# ***** DA COMPLETARE ******  con i file da consegnare
# primo frammento
FILE_DA_CONSEGNARE1=./genList.c ./genHash.c 


# secondo frammento
FILE_DA_CONSEGNARE2=./logpro 



# Compiler flags
CFLAGS = -Wall -pedantic -g 
# aggiungere -lgcov o -fprofile-arcs al linking

# Compilatore
CC = gcc


# Librerie
LIBDIR = ../lib
LIBS = -L $(LIBDIR)

# Nome libreria
LIBNAME = libmsg.a

# Lista degli object files (** DA COMPLETARE ***)
OBJS = genList.o genHash.o

# nomi eseguibili test primo frammento
exe1 = msg_test1
exe2 = msg_test2


# phony targets
.PHONY: clean lib test11 test12 docu consegna1
.PHONY: test21 consegna2


# creazione libreria
lib:  $(OBJS)
	-rm  -f $(LIBDIR)/$(LIBNAME)
	ar -r $(LIBNAME) $(OBJS)
	cp $(LIBNAME) $(LIBDIR)

# eseguibile di test 1 (primo frammento)
$(exe1): genList.o test-genList.o
	$(CC) -o $@ $^ 

# eseguibile di test 2 (primo frammento)
$(exe2): $(OBJS) test-genHash.o
	$(CC) -o $@ $^ 

# dipendenze oggetto main di test 11
test-genList.o: test-genList.c genList.h
	$(CC) $(CFLAGS) -c $<

# dipendenze oggetto main di test 12
test-genHash.o: test-genHash.c genHash.h genList.h
	$(CC) $(CFLAGS) -c $<

# make rule per gli altri .o del primo frammento (***DA COMPLETARE***)





msgserv: msgserv.o comsock.o \
	$(CC) -o $@ $^ $(LIBS) -lmsg -lpthread

msgcli: msgcli.o comsock.o
	$(CC) -o $@ $^ $(LIBS) -lmsg -lpthread

# make rule per i .o del terzo frammento (***DA COMPLETARE***)










########### NON MODIFICARE DA QUA IN POI ################
# genera la documentazione con doxygen
docu: ../doc/Doxyfile
	make -C ../doc

#ripulisce
clean:
	-rm -f *.o *.~

# primo test primo frammento
test11: 
	make clean
	make $(exe1)
	echo MALLOC_TRACE e\' $(MALLOC_TRACE)
	@echo MALLOC_TRACE deve essere settata a \"./.mtrace\"
	-rm -f ./.mtrace
	./$(exe1)
	mtrace ./$(exe1) ./.mtrace
	@echo -e "\a\n\t\t *** Test 1-1 superato! ***\n"

# secondo test primo frammento
test12: 
	make clean
	make $(exe2)
	echo MALLOC_TRACE e\' $(MALLOC_TRACE)
	@echo MALLOC_TRACE deve essere settata a \"./.mtrace\"
	-rm -f ./.mtrace
	./$(exe2)
	mtrace ./$(exe2) ./.mtrace
	@echo -e "\a\n\t\t *** Test 1-2 superato! ***\n"

# test secondo frammento
test21: 
	cp DATA/logtest.* .            
	chmod 644 ./logtest.*       
	./logpro ./logtest.dat > ./outtest    
	diff ./outtest ./logtest.check  
	@echo -e "\a\n\t\t *** Test 2-1 superato! ***\n"


# target di consegna primo frammento
SUBJECT1="lso10: consegna primo frammento"
consegna1: 
	make test11
	make test12
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f1.tar ./gruppo.txt ./Makefile $(FILE_DA_CONSEGNARE1)
	mpack -s $(SUBJECT1) ./$(USER)-f1.tar  susanna@di.unipi.it
	@echo -e "\a\n\t\t *** Frammento 1 consegnato  ***\n"

# target di consegna secondo frammento
SUBJECT2="lso10: consegna secondo frammento"
consegna2: 
	make test21
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f2.tar ./gruppo.txt $(FILE_DA_CONSEGNARE2)
	mpack -s $(SUBJECT2) ./$(USER)-f2.tar  susanna@di.unipi.it
	@echo -e "\a\n\t\t *** Frammento 2 consegnato  ***\n"

